﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Av.Utils;

namespace JrGreenMonster
{

    public class CstSignalItem
    {
        public string CstName { get; set; }
        public string AssetName { get; set; }
        public DateTime Start { get; set; }
        public decimal Price { get; set; }

        public string ToCsv()
        {
            return new StringBuilder().AppendFormat("{0},{1},{2},{3},{4},{5}", CstName, AssetName, "", "", "", Price).ToString();
        }
    }

    public class IttcDataFormatter
    {
        /// <summary>
        /// Parses CSV file and returns array of CstSignalItem
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static CstSignalItem[] Parse(string filename)
        {
            CstSignalItem item = null;
            List<CstSignalItem> ar = new List<CstSignalItem>();
            string[] lines = File.ReadAllLines(filename);

            ICstSignalParserBase parser = GetCstSignalsParser(lines[0].Split(",".ToCharArray()).Length);

            for(int i = 0; i < lines.Length; i++)
            {
                if(lines[i].Trim().Length > 0)
                {
                    item = parser.ParseLine(lines[i]);

                    //f.Add("l_" + i, lines[i].Trim());
                    ar.Add(item);
                }
            }

            return ar.ToArray();
        }

        #region CST signals parser factory

        protected static ICstSignalParserBase GetCstSignalsParser(int length)
        {
            if(length > 6)
            {
                return new CstSignalNewParser();
            }

            return new CstSignalOldParser();
        }

        /// <summary>
        /// CST signals parser interface
        /// </summary>
        protected interface ICstSignalParserBase
        {
            CstSignalItem ParseLine(string s);
        }

        /// <summary>
        /// Parses old format where price is one field
        /// </summary>
        protected class CstSignalOldParser : ICstSignalParserBase
        {

            /// <summary>
            /// Should parse line in format: <![CDATA[ <CST>,<ASSET>,..,...,...,<PRICE> ]]>
            /// </summary>
            /// <param name="s"></param>
            public CstSignalItem ParseLine(string s)
            {
                CstSignalItem cst = new CstSignalItem();

                try
                {
                    string[] ar = s.Split(",".ToCharArray());
                    cst.CstName = ar[0].Trim();
                    cst.AssetName = ar[1].Trim();
                    cst.Price = Decimal.Parse(ar[5]);
                } catch(Exception ex)
                {
                    Log4cs.Log(Importance.Debug, ex.ToString());
                }

                return cst;
            }
        }

        protected class CstSignalNewParser : ICstSignalParserBase
        {

            /// <summary>
            /// Should parse line in format: <![CDATA[ <CST>,<ASSET>,..,...,...,...,...,<PRICE_MAJOR>,<PRICE_MINOR> ]]>
            /// </summary>
            /// <param name="s"></param>
            public CstSignalItem ParseLine(string s)
            {
                CstSignalItem cst = new CstSignalItem();

                try
                {
                    string[] ar = s.Split(",".ToCharArray());
                    cst.CstName = ar[0].Trim();
                    cst.AssetName = ar[1].Trim();
                    cst.Price = Decimal.Parse(ar[7] + "," + ar[8]);
                } catch(Exception ex)
                {
                    Log4cs.Log(Importance.Debug, ex.ToString());
                }

                return cst;
            }

        }


        #endregion

    }
}
